package com.speily.cloud.mall.framework;

import com.speily.cloud.mall.common.AjaxResult;
import com.speily.cloud.mall.common.RespEnum;
import com.speily.cloud.storage.exception.StorageException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ExceptionHandler
 *
 * @author : SPL
 * @since : 2021-04-28 10:33
 **/
@RestControllerAdvice
@Slf4j
public class GlabExceptionHandler {

    /**
     * RPC调用异常
     */
    @ExceptionHandler(value = RpcException.class)
    public Object handle(RpcException e) {
        log.error("RPC调用异常：{}", e.getMessage());
        return AjaxResult.error(RespEnum.RPC_ERROR);
    }

    /**
     * StorageException
     */
    @ExceptionHandler(value = StorageException.class)
    public Object exceptionHandlerL(StorageException e) {
        log.error("库存服务异常", e);
        return AjaxResult.error(RespEnum.STORAGE_ERROR);
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    public Object exceptionHandlerL(Exception e) {
        log.error("系统错误:", e);
        return AjaxResult.error(RespEnum.UNKNOWN_ERROR);
    }

    //------------- HTTP协议相关 ----------------------

    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public Object eh(HttpRequestMethodNotSupportedException e) {
        log.error("系统错误:", e);
        return AjaxResult.error(400, e.getMessage());
    }


}
